*********************************************
*** Code for replication of main figures in "Police Violence Reduces Trust in the Police Among Black Residents"
*** Jonathan Ben-Menachem and Gerard Torrats-Espinosa
*** PLOS One, 2024
*********************************************

use "ben-menachem-torrats-espinosa-police-trust-plosone-2024.dta", clear


local controls  d_dev_desktop   d_hs d_somecol d_assoc d_ba d_grad  d_owner d_employed d_5years d_numchild 


foreach g in  black white hispanic    bl1844  wh1844  bl4599  wh4599    blarr  wharr  blnoarr  whnoarr  blmal whmal  blfem whfem   {
	foreach mod in reg   {
		foreach o in  pol  {
			foreach wpre in  4  {
				foreach wpost in  1 2  3 4    {
					
				local sample ((weeks >= -`wpre' & weeks <= -1 ) | weeks == `wpost') & d_`g' ==1  
	
					
				gen post_`mod'_`o'_`wpre'`g'`wpost' = post
				sum o_`o'_great_some if `sample' 
				local lab`wpost' `" "`wpost'w"  "'

				`mod' o_`o'_great_some post_`mod'_`o'_`wpre'`g'`wpost'  [aw=1]  if `sample',r
				estimates store `mod'_`o'_`wpre'`g'`wpost'ncro

				`mod' o_`o'_great_some post_`mod'_`o'_`wpre'`g'`wpost' `controls'  [aw=1]  if `sample',r
				estimates store `mod'_`o'_`wpre'`g'`wpost'wcro					
				
}



sum o_`o'_great_some if ((weeks >= -`wpre' & weeks <= -1 ) )  &  d_`g' ==1  

gen baseline_`o'_`wpre'_`g'_`mod' = `r(mean)'
format baseline_`o'_`wpre'_`g'_`mod' %9.2f 
tostring baseline_`o'_`wpre'_`g'_`mod' , replace u force
destring baseline_`o'_`wpre'_`g'_`mod',replace
format baseline_`o'_`wpre'_`g'_`mod' %9.2f 
sum  baseline_`o'_`wpre'_`g'_`mod'
local baseline_`o'_`wpre'_`g'_`mod' : di %9.2f  "`r(mean)'"

 coefplot ///
(`mod'_`o'_`wpre'`g'*ncro,lpatt(solid) lcol(blue) ciopts(lpatt(solid)  lcol(blue)) mlabposition(12) mlabcolor(blue) msize(3) msymbol(circle) mcolor(blue)  mfcolor(white) offset(-0.15) ) ///
 (`mod'_`o'_`wpre'`g'*wcro, lpatt(solid) lcol(black) ciopts(lpatt(solid) lcol(black)) mlabposition(6) msize(3) msymbol(circle) mcolor(black)  mfcolor(black) offset(0.15)) ///
, ///
	scheme(s1mono)  plotregion(lcolor(none)) /// 
	keep(post_*) ///
	yline(0,lcolor(red)) ///
	vertical ///
	 levels(95) ///
	 mlabel format(%9.2f)  mlabsize(vlarge) ///
	xtitle(" " "Weeks after the shooting", size(huge)) ///
	xlabel(1 "1w" 2 "2w"  3 "3w" 4 "4w"    ,labsize(huge)  ) ///
	ytitle("Change in probability" ,  size(huge)) ///
	ylabel(-.6(.3).6,labsize(huge)  format(%9.2g)) ///
	yscale(range(-.65 .65)) ///
	legend(order(2 "Without controls" 4 "With controls") region(lwidth(none)) size(huge)) 
	
	graph export coefplot-`o'-`g'-`mod'-c`wpre'w-robust.pdf,replace	
	
	estimates clear 
	
			}
		}
	}
}
